<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="库存日期" prop="stockDate">
        <el-date-picker
          clearable
          v-model="queryParams.stockDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择库存日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="店铺名称" prop="shop">
        <el-input
          v-model="queryParams.shop"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品库存" prop="productStock">
        <el-input
          v-model="queryParams.productStock"
          placeholder="请输入产品库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配件库存" prop="accessoriesStock">
        <el-input
          v-model="queryParams.accessoriesStock"
          placeholder="请输入配件库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总库存" prop="totalStock">
        <el-input
          v-model="queryParams.totalStock"
          placeholder="请输入总库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品库存总金额" prop="productStockAmount">
        <el-input
          v-model="queryParams.productStockAmount"
          placeholder="请输入产品库存总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配件库存总金额" prop="accessoriesStockAmount">
        <el-input
          v-model="queryParams.accessoriesStockAmount"
          placeholder="请输入配件库存总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存总金额" prop="totalStockAmount">
        <el-input
          v-model="queryParams.totalStockAmount"
          placeholder="请输入库存总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品库存周转率" prop="productStockRotationRate">
        <el-input
          v-model="queryParams.productStockRotationRate"
          placeholder="请输入产品库存周转率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配件库存周转率" prop="accessoriesStockRotationRate">
        <el-input
          v-model="queryParams.accessoriesStockRotationRate"
          placeholder="请输入配件库存周转率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存周转率" prop="totalStockRotationRate">
        <el-input
          v-model="queryParams.totalStockRotationRate"
          placeholder="请输入库存周转率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
        <el-input v-model="queryParams.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <timeSearch
      @getDateArr="getDateArr"
      @changDay="changDay"
      :timeArr="timeArr"
      :dayActive="this.dayActive"
      :dayLimit="1095"
    />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="display: none">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:BiShopInventoryDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5" style="display: none">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:BiShopInventoryDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5" style="display: none">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:BiShopInventoryDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:BiShopInventoryDwd:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefreshCache"
          >同步数据</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefreshCacheRate"
          >同步周转率数据</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div ref="tableDom">
      <el-table
        v-loading="loading"
        :maxHeight="BiShopInventoryDwdList.length ? tableHeight : 'auto'"
        :data="BiShopInventoryDwdList"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column width="140" type="selection" width="55" align="center" /> -->
        <el-table-column
          width="140"
          label="店铺销售明细ID"
          align="center"
          prop="id"
        />
        <el-table-column
          width="140"
          label="库存日期"
          align="center"
          prop="stockDate"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.stockDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="140"
          label="店铺名称"
          align="center"
          prop="shop"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          width="140"
          label="产品库存"
          align="center"
          prop="productStock"
        />
        <el-table-column
          width="140"
          label="配件库存"
          align="center"
          prop="accessoriesStock"
        />
        <el-table-column
          width="140"
          label="总库存"
          align="center"
          prop="totalStock"
        />
        <el-table-column
          width="140"
          label="产品库存总金额"
          align="center"
          prop="productStockAmount"
        />
        <el-table-column
          width="140"
          label="配件库存总金额"
          align="center"
          prop="accessoriesStockAmount"
        />
        <el-table-column
          width="140"
          label="库存总金额"
          align="center"
          prop="totalStockAmount"
        />
        <el-table-column
          width="140"
          label="产品库存周转率"
          align="center"
          prop="productStockRotationRate"
        />
        <el-table-column
          width="140"
          label="配件库存周转率"
          align="center"
          prop="accessoriesStockRotationRate"
        />
        <el-table-column
          width="140"
          label="库存周转率"
          align="center"
          prop="totalStockRotationRate"
        />
        <!-- <el-table-column width="140" label="逻辑删除;0=未删除；1=已删除" align="center" prop="isDelete" />
      <el-table-column width="140" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['product:BiShopInventoryDwd:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['product:BiShopInventoryDwd:remove']">删除</el-button>
        </template>
      </el-table-column> -->
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改TID5店铺库存日报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库存日期" prop="stockDate">
          <el-date-picker
            clearable
            v-model="form.stockDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择库存日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺名称" prop="shop">
          <el-input v-model="form.shop" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="产品库存" prop="productStock">
          <el-input v-model="form.productStock" placeholder="请输入产品库存" />
        </el-form-item>
        <el-form-item label="配件库存" prop="accessoriesStock">
          <el-input
            v-model="form.accessoriesStock"
            placeholder="请输入配件库存"
          />
        </el-form-item>
        <el-form-item label="总库存" prop="totalStock">
          <el-input v-model="form.totalStock" placeholder="请输入总库存" />
        </el-form-item>
        <el-form-item label="产品库存总金额" prop="productStockAmount">
          <el-input
            v-model="form.productStockAmount"
            placeholder="请输入产品库存总金额"
          />
        </el-form-item>
        <el-form-item label="配件库存总金额" prop="accessoriesStockAmount">
          <el-input
            v-model="form.accessoriesStockAmount"
            placeholder="请输入配件库存总金额"
          />
        </el-form-item>
        <el-form-item label="库存总金额" prop="totalStockAmount">
          <el-input
            v-model="form.totalStockAmount"
            placeholder="请输入库存总金额"
          />
        </el-form-item>
        <el-form-item label="产品库存周转率" prop="productStockRotationRate">
          <el-input
            v-model="form.productStockRotationRate"
            placeholder="请输入产品库存周转率"
          />
        </el-form-item>
        <el-form-item
          label="配件库存周转率"
          prop="accessoriesStockRotationRate"
        >
          <el-input
            v-model="form.accessoriesStockRotationRate"
            placeholder="请输入配件库存周转率"
          />
        </el-form-item>
        <el-form-item label="库存周转率" prop="totalStockRotationRate">
          <el-input
            v-model="form.totalStockRotationRate"
            placeholder="请输入库存周转率"
          />
        </el-form-item>
        <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import timeSearch from "@/views/chart/day/day.vue";
import {
  listBiShopInventoryDwd,
  getBiShopInventoryDwd,
  delBiShopInventoryDwd,
  addBiShopInventoryDwd,
  updateBiShopInventoryDwd,
  shopInventoryData,
  updateStockRotationRate,
} from "@/api/sku/BiShopInventoryDwd";

export default {
  name: "BiShopInventoryDwd",
  components: { timeSearch },
  data() {
    return {
      dayActive: 2,
      timeArr: [
        { value: "2", label: "昨天" },
        { value: "7", label: "最近7天" },
        { value: "30", label: "最近30天" },
        { value: "365", label: "最近一年" },
        { value: "1", label: "日" },
      ],
      timeGap: { startTime: "", endTime: "" },
      isOpen: false,
      //纵向滚动条
      tableHeight: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // TID5店铺库存日报表格数据
      BiShopInventoryDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockDate: null,
        shop: null,
        productStock: null,
        accessoriesStock: null,
        totalStock: null,
        productStockAmount: null,
        accessoriesStockAmount: null,
        totalStockAmount: null,
        productStockRotationRate: null,
        accessoriesStockRotationRate: null,
        totalStockRotationRate: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shop: [
          { required: true, message: "店铺名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    var _this = this;
    this.resizeTable();
    window.onresize = function () {
      _this.resizeTable();
    };
  },
  //记得清空  如果在keepalive中 则使用activated  deactivated
  destroyed() {
    window.onresize = null;
  },
  methods: {
    /** 刷新缓存按钮操作 Warning*/
    handleRefreshCache() {
      this.$modal.msgWarning("数据正在同步，请稍候");
      const obj = { updateTime: this.timeGap.startTime };
      shopInventoryData(obj).then(() => {
        this.$modal.msgSuccess("同步数据成功");
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    /** 刷新缓存按钮操作 Warning*/
    handleRefreshCacheRate() {
      this.$modal.msgWarning("数据正在同步，请稍候");
      const obj = { updateTime: this.timeGap.startTime };
      updateStockRotationRate().then(() => {
        this.$modal.msgSuccess("同步数据成功");
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    //时间控件
    changDay(val) {
      this.dayActive = val;
      this.timeGap.startTime = val[0];
      this.timeGap.endTime = val[1];
    },
    getDateArr(value) {
      this.timeGap.startTime = value[0];
      this.timeGap.endTime = value[1];
      console.log(JSON.stringify(this.timeGap));
      // this.queryParams.payDayStart = this.timeGap.startTime
      // this.queryParams.payDayEnd = this.timeGap.endTime

      // this.getList()

      this.dayActive = 1;
    },
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    // table自动高度
    returnTableHeight(ref, bottomOffset) {
      let height = null;
      height =
        window.innerHeight - ref.getBoundingClientRect().top - bottomOffset;
      return height;
    },

    resizeTable() {
      this.$nextTick(() => {
        this.tableHeight = this.returnTableHeight(this.$refs.tableDom, 100);
      }, 0);
    },
    /** 查询TID5店铺库存日报列表 */
    getList() {
      this.loading = true;
      listBiShopInventoryDwd(this.queryParams).then((response) => {
        this.BiShopInventoryDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockDate: null,
        shop: null,
        productStock: null,
        accessoriesStock: null,
        totalStock: null,
        productStockAmount: null,
        accessoriesStockAmount: null,
        totalStockAmount: null,
        productStockRotationRate: null,
        accessoriesStockRotationRate: null,
        totalStockRotationRate: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加TID5店铺库存日报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiShopInventoryDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改TID5店铺库存日报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiShopInventoryDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiShopInventoryDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除TID5店铺库存日报编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiShopInventoryDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "product/BiShopInventoryDwd/export",
        {
          ...this.queryParams,
        },
        `BiShopInventoryDwd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
